package com.wandian.util;

import java.io.PrintStream;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class TimeUtil
{
	public static long getSysCurTimeMillis()
	{
		return getCalendar().getTimeInMillis();
	}

	public static long getSysCurSeconds()
	{
		return getCalendar().getTimeInMillis() / 1000L;
	}

	public static Timestamp getSysteCurTime()
	{
		Timestamp ts = new Timestamp(getCalendar().getTimeInMillis());
		return ts;
	}

	public static Timestamp getSysMonth() {
		Calendar now = getCalendar();
		now.set(5, 1);
		now.set(11, 0);
		now.set(12, 0);
		now.set(13, 0);
		now.set(14, 0);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		formatter.format(now.getTime());
		return new Timestamp(now.getTimeInMillis());
	}

	public static long getDateToSeconds(Date date)
	{
		return getCalendar(date).getTimeInMillis() / 1000L;
	}

	public static int getTimeToSeconds(Time time)
	{
		if (time != null)
			return time.getHours() * 3600 + time.getMinutes() * 60 + time.getSeconds();
		return 0;
	}

	public static Time getSecondsToTime(int seconds) {
		Time time = new Time(seconds / 3600, seconds % 3600 / 60, seconds % 3600 % 60);
		return time;
	}

	public static int getSysTimeSeconds()
	{
		Calendar cal = getCalendar();
		return cal.get(11) * 3600 + cal.get(12) * 60 + cal.get(13);
	}

	public static long getDateToMillis(Date date)
	{
		return getCalendar(date).getTimeInMillis();
	}

	public static int getCurrentHour()
	{
		return getCalendar().get(11);
	}

	public static int getCurrentDay()
	{
		return getCalendar().get(5);
	}

	public static Timestamp getMillisToDate(long value)
	{
		return new Timestamp(value);
	}

	public static Date addSystemCurTime(int type, int value)
	{
		Calendar cal = getCalendar();
		switch (type) {
		case 5:
			cal.add(5, value);
			break;
		case 10:
			cal.add(10, value);
			break;
		case 12:
			cal.add(12, value);
			break;
		case 13:
			cal.add(13, value);
			break;
		case 14:
			cal.add(14, value);
			break;
		case 6:

		case 7:

		case 8:

		case 9:

		case 11:

		default:
			System.err.println("当前类型不存在！");
		}

		return new Date(cal.getTimeInMillis());
	}

	public static Date getNextDate() {
		Calendar cal = getCalendar();
		cal.add(5, 1);
		cal.set(11, 0);
		cal.set(12, 0);
		cal.set(13, 0);
		cal.add(14, 0);
		return new Date(cal.getTimeInMillis());
	}

	public static String getDateFormat(Date date)
	{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String ctime = formatter.format(date);
		return ctime;
	}

	public static Timestamp getDefaultDate()
	{
		Date defaultDate = null;
		try {
			defaultDate = (Date) new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parseObject("2000-01-01 00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Timestamp(defaultDate.getTime());
	}

	public static Timestamp getDefaultMaxDate()
	{
		Date defaultDate = null;
		try {
			defaultDate = (Date) new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parseObject("2999-01-01 00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Timestamp(defaultDate.getTime());
	}

	public static boolean dateCompare(Date date)
	{
		if (date == null)
			return false;
		Calendar now = getCalendar();
		Calendar other = getCalendar(date);
		return dateCompare(now, other) == 0;
	}

	public static boolean dataCompare5(Date date) {
		if (date == null)
			return false;
		Calendar now = getCalendar();
		now.add(11, -5);
		Calendar other = getCalendar(date);
		other.add(11, -5);

		return dateCompare(now, other) == 0;
	}

	public static boolean dataCompare(Date date1, Date date2)
	{
		if ((date1 == null) || (date2 == null))
			return false;
		Calendar c1 = getCalendar(date1);
		Calendar c2 = getCalendar(date2);
		return dateCompare(c1, c2) == 0;
	}

	public static int dateCompare(Calendar startDate, Calendar endDate)
	{
		startDate.set(11, 0);
		startDate.set(12, 0);
		startDate.set(13, 0);
		startDate.set(14, 0);

		endDate.set(11, 0);
		endDate.set(12, 0);
		endDate.set(13, 0);
		endDate.set(14, 0);

		int day = (int) (endDate.getTimeInMillis() / 1000L / 60L / 60L / 24L - startDate.getTimeInMillis() / 1000L / 60L / 60L / 24L);
		return day;
	}

	public static boolean monthCompare(Date date)
	{
		if (date == null)
			return false;
		Calendar now = getCalendar();
		Calendar other = getCalendar(date);
		int nowMonth = now.get(2) + 1;
		int otherMonth = other.get(2) + 1;
		return otherMonth - nowMonth == 0;
	}

	public static int monthDays()
	{
		Calendar now = getCalendar();
		return now.getActualMaximum(5);
	}

	public static int monthDay()
	{
		Calendar now = getCalendar();
		return now.get(5);
	}

	public static void setAASRefreshTime(int hour, Calendar refreshTime)
	{
		refreshTime.setTime(getSysteCurTime());
		refreshTime.set(11, hour);
		refreshTime.set(12, 0);
		refreshTime.set(13, 0);
	}

	public static long calcDistanceMillis(Date startTime, Date endTime) {
		long startSecond = getDateToSeconds(startTime);
		long endSecond = getDateToSeconds(endTime);
		return (endSecond - startSecond) * 1000L;
	}

	public static boolean isInterval(Date startDate, int interval)
	{
		return dataCompare5(startDate);
	}

	public static int timeToFrame(int secondTime) {
		return secondTime * 25 / 1000;
	}

	public static String getSignStr() {
		String[] strs = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
				"8", "9" };

		ThreadSafeRandom random = new ThreadSafeRandom();
		String signStr = "";
		for (int i = 0; i < 6; i++) {
			int j = random.next(strs.length);
			signStr = signStr + strs[j];
		}

		return signStr;
	}

	private static Calendar getCalendar()
	{
		Calendar nowCalendar = Calendar.getInstance();
		nowCalendar.setTime(new Date());
		return nowCalendar;
	}

	private static Calendar getCalendar(Date date)
	{
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		return calendar;
	}

	public static Timestamp getCalendarToDate(Calendar calendar) {
		if (calendar != null)
			return new Timestamp(getCalendar().getTimeInMillis());
		return null;
	}

}
